
package net.lab0.fractal.concurrent;


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import net.lab0.fractal.exception.IllegalOperationException;


public class MainThreadPoolExecutor
{
    private static ThreadPoolExecutor instance;
    
    public static synchronized void init(int corePoolSize, int maximumPoolSize, int keepAliveTime, TimeUnit unit)
    throws IllegalOperationException
    {
        if (instance == null)
        {
            instance = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>());
        }
        else
        {
            throw new IllegalOperationException("Main thread pool already initialized");
        }
    }
    
    public static synchronized ThreadPoolExecutor getInstance()
    {
        if (instance == null)
        {
            instance = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(), 8, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
        }
        return instance;
    }
}
